/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package my.cbr.database;

import com.mysql.management.MysqldResource;
import com.mysql.management.MysqldResourceI;
import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * @author Geir Ole
 */
public class MySQLDBServer {
    static String username = "root";
    static String password = "";
    static Integer port = 1337;
    public static String database = "FishFarmingCaseBase";
    static File databasedir = new File("C:\\dbtemp", "mxj-tmp");
    static Connection connection;
    static boolean initialized = false;
    static MysqldResource mysqldResource;

    public static void init()
    {
        if(MySQLDBServer.initialized)
            return;
        org.apache.commons.logging.LogFactory.getLog(MySQLDBServer.class).info("Starting data base ...");
        mysqldResource = startDatabase(databasedir, port, username, password);
        MySQLDBServer.initialized = true;
    }
    
    public static void setConnection() {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            Properties p = new Properties();
            p.setProperty("user", MySQLDBServer.username);
            p.setProperty("password", MySQLDBServer.password);
            p.setProperty("createDatabaseIfNotExist", "true");
            p.setProperty("server.basedir",databasedir.toString());
            MySQLDBServer.connection  = 
                    DriverManager.getConnection("jdbc:mysql://localhost:"+port+"/"+MySQLDBServer.database, p);
            
        }
        catch (Exception e)
        {
            org.apache.commons.logging.LogFactory.getLog(MySQLDBServer.class).error(e);
        }
    }

    public static Connection getConnection() {
        return connection;
    }

    public static void setConnection(Connection connection) {
        MySQLDBServer.connection = connection;
    }
    
    public static MysqldResource startDatabase(File databaseDir, int port,
            String userName, String password) {
        MysqldResource mysqldResource = new MysqldResource(databaseDir);

        Map database_options = new HashMap();
        database_options.put(MysqldResourceI.PORT, Integer.toString(port));
        database_options.put(MysqldResourceI.INITIALIZE_USER, "true");
        database_options.put(MysqldResourceI.INITIALIZE_USER_NAME, userName);
        database_options.put(MysqldResourceI.INITIALIZE_PASSWORD, password);

        mysqldResource.start("test-mysqld-thread", database_options);

        if (!mysqldResource.isRunning()) {
            throw new RuntimeException("MySQL did not start.");
        }

        System.out.println("MySQL is running.");

        return mysqldResource;
    }
    
    public static void shutdown()
    {
        if(MySQLDBServer.initialized)
        {
            try {
                mysqldResource.shutdown();
            } catch (Exception e) {
                org.apache.commons.logging.LogFactory.getLog(MySQLDBServer.class).error(e);
            }
            MySQLDBServer.initialized = false;
        }
    }

}
